package com.cloud.pole.shop.controller.shops.admin;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.common.util.TreeUtil;
import com.cloud.pole.shop.biz.shops.ShopShopsBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsPayMethodBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsPayTypeBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsPayTypeStageNumBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsGroup;
import com.cloud.pole.shop.model.entity.shops.ShopShops;
import com.cloud.pole.shop.model.entity.shops.ShopShopsPayMethod;
import com.cloud.pole.shop.model.entity.shops.ShopShopsPayType;
import com.cloud.pole.shop.model.entity.shops.ShopShopsPayTypeStageNum;
import com.cloud.pole.shop.model.entity.shops.ShopShopsWithBLOBs;
import com.cloud.pole.shop.model.vo.shops.ShopShopsTree;

import com.cloud.pole.shop.service.shops.ShopShopsService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;

/*
*2019年7月1日
*cloud-shop
*author:frank-chen
*/
@RestController
@RequestMapping("/shops/admin")
@Slf4j
public class ShopsAdminController extends BaseController<ShopShopsBiz, ShopShops> {
	@Autowired
	private ShopShopsService shopShopsService;
	@Autowired
	private ShopShopsPayMethodBiz shopShopsPayMethodBiz;
	@Autowired
	private ShopShopsPayTypeBiz shopShopsPayTypeBiz;
	@Autowired
	private ShopShopsPayTypeStageNumBiz shopShopsPayTypeStageNumBiz;

	/**
	 * 分页商品
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @param mobile
	 * @param templateName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	@ResponseBody
	public ApiRestResponse<ShopShopsWithBLOBs> list(Integer pageNo, Integer pageSize, String shopName) {
		pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
		pageSize = (pageSize == null || pageSize < 1) ? 10 : pageSize;
		try {
			PageInfo<ShopShopsWithBLOBs> pageInfo = shopShopsService.selectPageShopAdminList(shopName, pageNo,
					pageSize);
			return ApiRestResponse.successOfPage("成功", (int) pageInfo.getTotal(), pageInfo.getPages(),
					pageInfo.getPageSize(), pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@RequestMapping(value = "/getAllShopList", method = RequestMethod.GET)
	@ResponseBody
	public ApiRestResponse<List<ShopShopsWithBLOBs>> getAllShopList(@RequestHeader(defaultValue = "") String token) {
		try {
			List<ShopShopsWithBLOBs> shopList = shopShopsService.getAllShopList(token);
			return ApiRestResponse.success("成功", shopList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.toString(), null);
		}
	}

	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	@ResponseBody
	public List<ShopShopsTree> getTree(@RequestHeader(defaultValue = "") String token, String title) {
		List<ShopShopsWithBLOBs> shopList = null;
		try {
			shopList = shopShopsService.getAllShopList(token);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
		}
		return getShopsTree(shopList, 0);
	}

	private List<ShopShopsTree> getShopsTree(List<ShopShopsWithBLOBs> list, int root) {
		List<ShopShopsTree> trees = new ArrayList<ShopShopsTree>();
		ShopShopsTree node = null;
		for (ShopShops shop : list) {
			node = new ShopShopsTree();
			node.setId(shop.getShopId());
			node.setParentId(0);
			node.setName(shop.getShopName());
			node.setMerchantId(shop.getMerchantId());
			trees.add(node);
		}
		return TreeUtil.bulid(trees, root);
	}

	@ResponseBody
	@PostMapping("/addShopShops")
	private ApiRestResponse<ShopShops> addShopShops(@RequestBody Map map) {
		try {
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			shopShopsService.addShopShops(token, map);
			return ApiRestResponse.success(200, "成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}

	}

	@ResponseBody
	@GetMapping("getShopsInfo/{id}")
	public ApiRestResponse<Map<String, Object>> updateShops(@PathVariable("id") String id) {
		try {
			Map<String, Object> objectMap = new HashMap<String, Object>();
			String msg = "成功";
			ShopShops selectByPrimaryKey = null;
			if (id != null) {
				selectByPrimaryKey = shopShopsService.selectByPrimaryKey(Integer.valueOf(id));
				List<ShopShopsPayMethod> selectListByShopId = shopShopsPayMethodBiz
						.selectListByShopId(Integer.valueOf(id));// 店铺的支付方式
				Integer shopShopsPayMethodId = null;
				for (ShopShopsPayMethod shopShopsPayMethod : selectListByShopId) {
					shopShopsPayMethodId = shopShopsPayMethod.getPayMethodId();

				}
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("shopId", Integer.valueOf(id));
				map.put("payMethodId", shopShopsPayMethodId);
				List<ShopShopsPayType> selectListByMap = shopShopsPayTypeBiz.selectListByMap(map);// 店铺的支付单位
				Integer shopShopsPayTypeId = null;
				for (ShopShopsPayType shopShopsPayType : selectListByMap) {

					shopShopsPayTypeId = shopShopsPayType.getPayTypeId();
				}
				Map<String, Object> selectMap = new HashMap<String, Object>();
				selectMap.put("shop_id", Integer.valueOf(id));
				selectMap.put("payTypeId", shopShopsPayTypeId);
				List<ShopShopsPayTypeStageNum> shopShopsPayTypeStageNum = shopShopsPayTypeStageNumBiz
						.selectListByMap(map); // 店铺分期支付的期数
				for (ShopShopsPayTypeStageNum shopsPayTypeStageNum : shopShopsPayTypeStageNum) {

				}
				objectMap.put("shopShops", selectByPrimaryKey);
				if (selectListByShopId.size() != 0 || selectListByShopId != null) {
					objectMap.put("shopShopsPayMethod", selectListByShopId);
				}
				if (selectListByMap.size() != 0 || selectListByMap != null) {
					objectMap.put("shopShopsPayType", selectListByMap);
				}
				if (shopShopsPayTypeStageNum.size() != 0 || shopShopsPayTypeStageNum != null) {
					objectMap.put("shopShopsPayTypeStageNum", shopShopsPayTypeStageNum);
				}
			} else {
				msg = "店铺编号不能为空";
			}
			return ApiRestResponse.success(msg, objectMap);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}

	}

	@ResponseBody
	@PutMapping("updateShops")
	public ApiRestResponse<ShopShops> updateShops(@RequestBody Map map) {
		try {
			Integer shopId = null;
			for (Object obj : map.keySet()) {
				shopId = (Integer) map.get("shopId");
			}
			String token = request.getHeader(CommonConstants.AUTH_USER_TOKEN_HEADER);
			ShopShops shopShops = shopShopsService.selectByPrimaryKey(shopId);
			if (shopShops != null) {
				shopShopsService.updateShopShops(token, map);
			}
			return ApiRestResponse.success(200, "成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return ApiRestResponse.fail(e.toString(), null);
		}
	
	}
	
}
